package org.shoukaiseki.aqoeous.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.shoukaiseki.aqoeous.config.Validator;
import org.shoukaiseki.aqoeous.constants.LoggerConstants;
import org.shoukaiseki.aqoeous.model.CustomQO;
import org.shoukaiseki.aqoeous.model.ResultModel;
import org.shoukaiseki.aqoeous.processor.CustomQOProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * org.shoukaiseki.aqoeous.controller.AqoeousController
 * <br>
 *
 * @author 蒋カイセキ    Japan-Tokyo  19-2-11 下午3:54<br>
 * ブログ http://www.shoukaiseki.top/<br>
 * E-メール jiang28555@Gmail.com<br>
 **/
@Controller
public class AqoeousController {

    final static Logger logger= LoggerConstants.AQOEOUS_LOGGER;

    @Resource
    private Validator validator;

    @Resource
    private CustomQOProcessor customQOProcessor;

    /**
     * 返回使用FastJson 转换时间格式为 yyyy-MM-dd HH:mm:ss
     * @param alias             后端没什么所用,方便前端显示时方便在network查看时更直观的看到属于哪个接口的连接
     * @param jsonStr
     * @param bindingResult
     * @return
     */
    @PostMapping(value = "/aqoeousDateFormat/{alias}")
    @ResponseBody
    public String aqoeousDateFormat(@PathVariable("alias")String alias,@RequestBody JSONArray jsonStr, BindingResult bindingResult){
        logger.debug("alias={}",alias);
        List<CustomQO> customQOList=null;
        if(jsonStr!=null){

            customQOList = jsonStr.toJavaList(CustomQO.class);
        }

        this.validate(customQOList,bindingResult);

        ResultModel resultModel = new ResultModel();
        if(customQOList==null){
            return JSONObject.toJSONStringWithDateFormat(resultModel, "yyyy-MM-dd HH:mm:ss");
        }


        Map<String, Object> customsPage = customQOProcessor.parserCustomQO(customQOList);

        resultModel.setData(customsPage);

        return JSONObject.toJSONStringWithDateFormat(resultModel, "yyyy-MM-dd HH:mm:ss");
    }

    @PostMapping(value = "/aqoeous")
    @ResponseBody
    public ResultModel queryObject(@RequestBody JSONArray jsonStr, BindingResult bindingResult){
        List<CustomQO> customQOList=null;
        if(jsonStr!=null){

            customQOList = jsonStr.toJavaList(CustomQO.class);
        }

        this.validate(customQOList,bindingResult);

        ResultModel resultModel = new ResultModel();
        if(customQOList==null){
            return resultModel;
        }


        Map<String, Object> customsPage = customQOProcessor.parserCustomQO(customQOList);

        resultModel.setData(customsPage);

        return resultModel;
    }

    void validate(List<CustomQO> customQOList, BindingResult bindingResult){
        validator.validate(customQOList,bindingResult);
        for (CustomQO customQO: customQOList) {
            validator.validate(customQO,bindingResult);
        }
    }
}
